<?php
/**
 * FactoryTrait.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/5/10
 * Time: 9:23
 */
namespace app\common;

trait FactoryTrait
{
    protected $instance = null;

    /**
     * @param $className
     * @param null $arg
     * @return object
     * @throws \ReflectionException
     */
    public static function createOrigin($className, $arg = null){
//        $newClassName = __NAMESPACE__."\\{$className}";
//        return new $newClassName();
        $ref = new \ReflectionClass($className);
        if($arg === null){
            return $ref->newInstance();
        }
        return $ref->newInstance($arg);
    }

    /**
     * FactoryTrait constructor.
     * @param $className
     * @throws \ReflectionException
     */
    public function __construct($className){
        $this->instance = self::createOrigin($className);
    }
}